/**
 * @file Storage 提供 localStorage 存储和读取功能.
 * @author liaoxuezhi@baidu.com
 */
/* eslint fecs-indent: [0, "space", 2, 2] */

import {
  TOGGLE_ASIDE_FOLDED,
  TOGGLE_ASIDE_NAV_OPEN,
  TOGGLE_HEADER_NAV_OPEN
} from './constants';
import * as storage from './storage';
import * as assign from 'object-assign';

export default function persistenceHandler(next) {
  return (reducer, initialState) => {
    const store = next(reducer, initialState);

    return assign({}, store, {
      dispatch: action => {
        store.dispatch(action);
        if (action.error) {
          return action;
        }

        const type =  action.type;
        const state = store.getState();

        switch (type) {

          case TOGGLE_ASIDE_FOLDED:
            storage.put('asideFolded', state.assist.asideFolded);
            break;

          /*case TOGGLE_ASIDE_NAV_OPEN:
            storage.put('asideNavOpened', state.assist.asideNavOpened);
            break;*/
        }

        return action;
      }
    });
  };
}
